package org.zlb.netty.server.serial;

import org.zlb.netty.dbo.SubReq;
import org.zlb.netty.dbo.SubResp;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class SubReqServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SubReq req = (SubReq)msg;
        System.out.println(req.getSubReqId());
        System.out.println(req.getUserName());
        System.out.println(req.getUserPhone());
        System.out.println(req.getAddress());
        ctx.writeAndFlush(resp(req.getSubReqId()));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    private SubResp resp(int reqId) {
        SubResp resp = new SubResp();
        resp.setSubReqId(reqId);
        resp.setRespCode(0);
        resp.setDesc("订购成功，3日后送达");
        
        return resp;
    }
}
